package logic

import (
	"context"
	"go.uber.org/zap"
	"smart-health-platforms/pkg"
	"user_srv/internal/svc"
	"user_srv/model"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type RecentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRecentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecentListLogic {
	return &RecentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 最近活动列表
func (l *RecentListLogic) RecentList(in *user.RecentListRequest) (*user.RecentListResponse, error) {
	var re model.RecentEvents
	re.UserId = int32(in.UserId)
	events, err := re.ListRecentEvents()
	if err != nil {
		pkg.Error("失败", zap.Error(err))
		return nil, err
	}
	var list []*user.RecentList
	for _, event := range events {
		list = append(list, &user.RecentList{
			GoodId:  int64(event.GoodId),
			OrderSn: event.OrderOn,
			Type:    int64(event.Type),
		})
	}
	return &user.RecentListResponse{
		List: list,
	}, nil
}
